home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Bug Proofing Visual Basic: A Guide to Error Handling and Prevention
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471323519
Publication Date: 11/01/98

Search this book:
 
Previous Table of Contents Next


CHAPTER 15
Profiling

The Professional and Enterprise editions of Visual Basic 4 and 5 come with Visual Basic Code Profiler (VBCP). VBCP lets you generate performance and usage statistics for your programs. These statistics allow you identify slow pieces of code and code that is not executed.

After you locate unused code, you can examine it to decide if there is a bug. If the program should use the code but does not, you can modify the program so it uses the code properly. If the program already uses the code but your test cases did not exercise it, you can update the tests so they give the program a more vigorous workout. Finally, if the unused code is unnecessary, you can remove it.

VBCP can also tell you which code takes most of the program’s time. You can then optimize that code to improve overall performance. By telling you which code takes the most time, the profiler lets you concentrate your optimization efforts on just that code. You do not waste time optimizing code that is already fast enough. You also do not add bugs by needlessly complicating that code.

Installing VB Code Profiler

In Visual Basic 4, VBCP comes in the Tools\Vbcp directory on the Visual Basic CD-ROM. It runs as an add-in within the Visual Basic development environment. To install VBCP in Visual Basic 4, follow these steps:

1.  Copy the file Vbcp16.exe (16-bit) or Vbcp32.exe (32-bit) from the CD-ROM to your hard disk. You can also copy the help file Vbcp.hlp if you like.
2.  Execute the .exe file you copied. This will register VBCP so you can run it from the Add-Ins menu.
3.  Run the Visual Basic development environment. In the Add-Ins menu, select the Add-In Manager command. Check the VB Code Profile and click the Ok button.

In Visual Basic 5, VBCP comes in the Tools\Unsupprt\Vbcp directory on the Visual Basic CD-ROM. To install VBCP in Visual Basic 5, follow these steps:

1.  Copy the file Vbcp.dll from the CD-ROM to your System directory. In Windows 95, that directory is Windows\System. In Windows NT, the System directory is Winnt\System.
2.  Register the DLL using RegSvr32.exe by executing the following command:
RegSvr32 vbcp.dll

If you do not already have RegSvr32 installed, you can find it in the Tools\RegUtils directory on the Visual Basic CD-ROM.
3.  Edit the file Vbaddin.ini in the Windows directory. In Windows 95, this directory is Windows. In Windows NT, the Windows directory is Winnt. Find the Add-Ins32 section of the file and add the line VBCP.VBCPClass=0 like this:
[Add-Ins32]
VBCP.VBCPClass=0
4.  Run the Visual Basic development environment. In the Add-Ins menu, select the Add-In Manager command. Check the VB Code Profile and click the Ok button.

Visual Basic 6 does not include VBCP. Instead the Enterprise Edition includes the Application Performance Explorer (APE). Unfortunately APE is designed to test the performance of the components in multi-tier client/server applications rather than the performance of code within a simple application. You can learn more about APE in the online help.

The following sections explain how to use VBCP in Visual Basic 5. The concepts apply to the Visual Basic 4 version, and even to Visual Basic 6 programs. For example, you could insert profiling code of your own in a Visual Basic 6 program. This would be a fairly involved project, though, so it is not described here.

How VBCP Works

VBCP adds profiling code to your Visual Basic project. It does nothing magical. It simply inserts statements between each of the statements in your code. The VBCP statements record timing and hit information in a database for later analysis. With enough time and effort, you could do something similar yourself.

The following code shows function TestFormat. This routine formats a double value so it has at most 12 characters, plus an optional sign.

Private Const TEXT_FORMAT = “0.############”
    :
‘ Return a textual representation of the value.
Private Function TestFormat(ByVal value As Double, _
     ByVal is_signed As Boolean) As String
Dim txt As String

     txt = Format$(Abs(value), TEXT_FORMAT)
     txt = Left$(txt, 12)
     If is_signed And value < 0 Then txt = “-” & txt
     TestFormat = txt
End Function

The following code shows how VBCP modifies this function. The calls to VBCP_Update and VBCP_UpdateIf record timing and hit count information when they are called.

‘ Return a textual representation of the value.
Private Function TestFormat(ByVal value As Double, _
    ByVal is_signed As Boolean) As String
  VBCP_Update 1, “TestFormat”, 1
Dim txt As String

    txt = Format$(Abs(value), TEXT_FORMAT)
  VBCP_Update 1, “TestFormat”, 2
    txt = Left$(txt, 12)
  VBCP_Update 1, “TestFormat”, 3
    If is_signed And value < 0 And _
   VBCP_UpdateIf(1, “TestFormat”, 4) Then txt = “-” & txt
    TestFormat = txt
   VBCP_Update 1, “TestFormat”, 5
End Function

Using VBCP

To use the code profiler, follow these steps:

1.  Save the project. VBCP requires that you save the project before profiling the code. If you have not saved the project, VBCP complains that the code is dirty.
2.  From the Add-Ins menu, select the VB Code Profiler command. This displays VBCP as shown in Figure 15.1.
3.  Select the modules you want profiled. You must include the startup form or module.
4.  Select the kind of information to profile. The choices are:
Line Timing. VBCP records the amount of time spent executing each line of code.\
Function Timing. VBCP records the amount of time spent executing each routine.
Line Hit Count. VBCP records the number of times each line is executed.
Function Hit Count. VBCP records the number of times each routine is executed.
5.  Click the Add Profiler Code button. At this point, VBCP adds the profile code between your Visual Basic statements.


Figure 15.1  Visual Basic Code Profiler.

6.  Run the program and exercise the code you want to study. You may need to execute fast code several times to get meaningful statistics.
7.  Analyze the results. This is described in more detail later.
8.  Click the Remove Profiler Code button. This makes VBCP remove the code it added to the project.
9.  Save the project. VBCP made changes so it will not profile the code again until you save it.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.